package com.hc.controller;

import com.hc.bean.Pager;
import com.hc.bean.Product;
import com.hc.bean.ShopCart;
import com.hc.bean.User;
import com.hc.service.ProductService;
import com.hc.service.ShopCartService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private ShopCartService shopCartService;

    /*商品添加*/
    /*查询所有用户*/
    @RequestMapping("/addProduct")
    @ResponseBody
    public Map<String, Object> addProduct() {
        Map<String, Object> resultMap = new HashMap<>();
        Product product1=new Product();
        product1.setProductName("珀莱雅双抗粉底液");
        product1.setProductPrice(53.99D);
        product1.setProductIntroduction("珀莱雅双抗粉底液中小样控油干油皮持久遮瑕提亮平价学生试用试色");
        product1.setProductType(1);
        product1.setProductStock(500);
        product1.setProductPicture("../img/like_01.png");
        product1.setProductState(1);
        if(productService.addProduct(product1)){
            resultMap.put("success", true);
            resultMap.put("message", "商品添加成功!");
        }else {
            resultMap.put("success", false);
            resultMap.put("message", "商品添加失败!");
        }
        return resultMap;
    }

    /*模糊分页查询*/
    @RequestMapping("/fuzzyPagingQuery")
    @ResponseBody
    public Map<String, Object> fuzzyPagingQuery(Product product) {
        Map<String, Object> resultMap = new HashMap<>();
        System.out.println(product);
        Pager pager=new Pager();
        pager.setPage(product.getPageIndex());
        pager.setPageRows(product.getPageRows());
        List<Product> productList=productService.fuzzyPagingQuery(product,pager);
//        List<Product> productList=productService.fuzzyQuery(product);
        resultMap.put("success", true);
        resultMap.put("message", "商品查询成功!");
        resultMap.put("productList", productList);
        resultMap.put("total",productService.fuzzyQuery(product).size());
        return resultMap;
    }

    /*加入购物车*/
    @RequestMapping("/addShopCartOrCollect")
    @ResponseBody
                                                /*需要加上 @RequestBody 才可以自动装配前端传过来的Jason数据*/
    public Map<String, Object> addShopCartOrCollect(@RequestBody ShopCart shopCart, HttpSession session) {
        System.out.println(session.getAttribute("user"));
        System.out.println(shopCart);

        Map<String, Object> resultMap = new HashMap<>();
        User user= (User) session.getAttribute("user");
        if(user!=null){
            /*
             * 判断商品是否已在购物车中
             * 1.不存在--直接添加
             * 2.已存在--
             *   1.isShopCart:
             *       true: 修改数量
             *       false:  说明该商品已被收藏 设置 isCisCollect为true,修改数量
             * */
            shopCart.setUserId(user.getUserId());
            ShopCart oldShopCart=shopCartService.getShopCartByPid(user.getUserId(),shopCart.getProductId());
            if(oldShopCart!=null){
                /*根据条件修改购物车信息*/
                /*商品在购物车表中,且需要再次加入购物车*/
                if(oldShopCart.getIsShopCart() & shopCart.getIsShopCart()){
                    /*商品已加入购物车 直接修改数量*/
                    oldShopCart.setProductNum(oldShopCart.getProductNum()+shopCart.getProductNum());
                    if(shopCartService.updateShopCart(oldShopCart)){
                        resultMap.put("success", true);
                        resultMap.put("message", "商品添加成功!");
                    }else {
                        resultMap.put("success", false);
                        resultMap.put("message", "1商品添加失败!");
                    }
                }else if(!oldShopCart.getIsShopCart() & shopCart.getIsShopCart()){
                    /*设置加入购物车标记*/
                    oldShopCart.setIsShopCart(true);
                    /*修改商品数量*/
                    if(oldShopCart.getProductNum()!=null){
                        oldShopCart.setProductNum(oldShopCart.getProductNum()+shopCart.getProductNum());
                    }else {
                        oldShopCart.setProductNum(shopCart.getProductNum());
                    }
                    if(shopCartService.updateShopCart(oldShopCart)){
                        resultMap.put("success", true);
                        resultMap.put("message", "商品添加成功!");
                    }else {
                        resultMap.put("success", false);
                        resultMap.put("message", "2商品添加失败!");
                    }
                }else if(!oldShopCart.getIsCollect() & shopCart.getIsCollect()){
                    /*添加收藏*/
                    oldShopCart.setIsCollect(true);
                    if(shopCartService.updateShopCart(oldShopCart)){
                        resultMap.put("success", true);
                        resultMap.put("message", "商品添加成功!");
                    }else {
                        resultMap.put("success", false);
                        resultMap.put("message", "3商品添加失败!");
                    }
                }else {
                    /*商品不在购物车表,直接添加*/
                    if(shopCartService.addShopCart(shopCart)){
                        resultMap.put("success", true);
                        resultMap.put("message", "商品添加成功!");
                    }else {
                        resultMap.put("success", false);
                        resultMap.put("message", "4商品添加失败!");
                    }
                }
            }else {
                /*商品不在购物车表,直接添加*/
                if(shopCartService.addShopCart(shopCart)){
                    resultMap.put("success", true);
                    resultMap.put("message", "商品添加成功!");
                }else {
                    resultMap.put("success", false);
                    resultMap.put("message", "4商品添加失败!");
                }
            }
        }else {
            resultMap.put("success", false);
            resultMap.put("message", "您还没有登录,请先登录!");
        }
        return resultMap;
    }

    /*显示商品详情*/
    @RequestMapping("/productDetail")
    @ResponseBody
    public Map<String, Object> productDetail(@RequestBody Product product, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>();
        System.out.println(product);
        product=productService.getProductById(product.getProductId());
        if(product!=null){
//            session.setAttribute("product",product);
            resultMap.put("success", true);
            resultMap.put("product",product);
            resultMap.put("message", "商品查询成功!");
        }else {
            resultMap.put("success", false);
            resultMap.put("message", "该商品不存在,请刷新页面!");
        }
        return resultMap;
    }

    /*查询所有用户*/
    @RequestMapping("/test")
    @ResponseBody
    public Map<String, Object> test() {
        Map<String, Object> resultMap = new HashMap<>();
        return resultMap;
    }
}
